home *** CD-ROM | disk | FTP | other *** search
/ C++ für Kids / C++ for kids.iso / SETUP / US / CBUILDER / DATA.Z / COMPLEX.H < prev    next >
C/C++ Source or Header  |  1997-02-13  |  50KB  |  1,733 lines

  1. /* complex.h
  2.  
  3.     Complex Number Library - Include File
  4.     class complex:  declarations for complex numbers.
  5.  
  6. All function names, member names, and operators have been borrowed
  7. from AT&T C++, except for the addition of:
  8.  
  9.     friend complex _RTLENTRY acos(complex _FAR &);
  10.     friend complex _RTLENTRY asin(complex _FAR &);
  11.     friend complex _RTLENTRY atan(complex _FAR &);
  12.     friend complex _RTLENTRY log10(complex _FAR &);
  13.     friend complex _RTLENTRY tan(complex _FAR &);
  14.     friend complex _RTLENTRY tanh(complex _FAR &);
  15.     complex _RTLENTRY operator+();
  16.     complex _RTLENTRY operator-();
  17. */
  18.  
  19. /*
  20.  *      C/C++ Run Time Library - Version 8.0
  21.  *
  22.  *      Copyright (c) 1990, 1997 by Borland International
  23.  *      All Rights Reserved.
  24.  *
  25.  */
  26. /* $Revision:   8.2  $ */
  27.  
  28. #ifndef __cplusplus
  29. #error Must use C++ for the type complex.
  30. #endif
  31.  
  32. #if !defined(__USING_STD_NAMES__)
  33.  
  34. #if !defined(__COMPLEX_H)
  35. #define __COMPLEX_H
  36.  
  37.  
  38. #if !defined(___DEFS_H)
  39. #include <_defs.h>
  40. #endif
  41.  
  42. #if !defined(__MATH_H)
  43. #include <math.h>
  44. #endif
  45.  
  46. #if !defined(__IOSTREAM_H)
  47. #include <iostream.h>
  48. #endif
  49.  
  50.  
  51. #if !defined(RC_INVOKED)
  52.  
  53. #if defined(__BCOPT__)
  54. #if !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
  55. #pragma option -po-     // disable Object data calling convention
  56. #endif
  57. #endif
  58.  
  59. #pragma option -Vo-
  60.  
  61. #if defined(__STDC__)
  62. #pragma warn -nak
  63. #endif
  64.  
  65. #endif  /* !RC_INVOKED */
  66.  
  67.  
  68. class _EXPCLASS complex {
  69.  
  70. public:
  71.     // constructors
  72.     _RTLENTRY complex(double __re_val, double __im_val=0);
  73.     _RTLENTRY complex();
  74.  
  75.     // complex manipulations
  76.     friend double  _RTLENTRY _EXPFUNC real(const complex _FAR &);   // the real part
  77.     friend double  _RTLENTRY _EXPFUNC imag(const complex _FAR &);   // the imaginary part
  78.     friend complex _RTLENTRY _EXPFUNC conj(const complex _FAR &);   // the complex conjugate
  79.     friend double  _RTLENTRY _EXPFUNC norm(const complex _FAR &);   // the square of the magnitude
  80.     friend double  _RTLENTRY _EXPFUNC arg(const complex _FAR &);    // the angle in the plane
  81.  
  82.     // Create a complex object given polar coordinates
  83.     friend complex _RTLENTRY _EXPFUNC polar(double __mag, double __angle=0);
  84.  
  85.     // Overloaded ANSI C math functions
  86.     friend double  _RTLENTRY _EXPFUNC abs(const complex _FAR &);
  87.     friend complex _RTLENTRY _EXPFUNC acos(const complex _FAR &);
  88.     friend complex _RTLENTRY _EXPFUNC asin(const complex _FAR &);
  89.     friend complex _RTLENTRY _EXPFUNC atan(const complex _FAR &);
  90.     friend complex _RTLENTRY _EXPFUNC cos(const complex _FAR &);
  91.     friend complex _RTLENTRY _EXPFUNC cosh(const complex _FAR &);
  92.     friend complex _RTLENTRY _EXPFUNC exp(const complex _FAR &);
  93.     friend complex _RTLENTRY _EXPFUNC log(const complex _FAR &);
  94.     friend complex _RTLENTRY _EXPFUNC log10(const complex _FAR &);
  95.     friend complex _RTLENTRY _EXPFUNC pow(const complex _FAR & __base, double __expon);
  96.     friend complex _RTLENTRY _EXPFUNC pow(double __base, const complex _FAR & __expon);
  97.     friend complex _RTLENTRY _EXPFUNC pow(const complex _FAR & __base, const complex _FAR & __expon);
  98.     friend complex _RTLENTRY _EXPFUNC sin(const complex _FAR &);
  99.     friend complex _RTLENTRY _EXPFUNC sinh(const complex _FAR &);
  100.     friend complex _RTLENTRY _EXPFUNC sqrt(const complex _FAR &);
  101.     friend complex _RTLENTRY _EXPFUNC tan(const complex _FAR &);
  102.     friend complex _RTLENTRY _EXPFUNC tanh(const complex _FAR &);
  103.  
  104.     // Binary Operator Functions
  105.     friend complex _RTLENTRY _EXPFUNC operator+(const complex _FAR &, const complex _FAR &);
  106.     friend complex _RTLENTRY _EXPFUNC operator+(double, const complex _FAR &);
  107.     friend complex _RTLENTRY _EXPFUNC operator+(const complex _FAR &, double);
  108.     friend complex _RTLENTRY _EXPFUNC operator-(const complex _FAR &, const complex _FAR &);
  109.     friend complex _RTLENTRY _EXPFUNC operator-(double, const complex _FAR &);
  110.     friend complex _RTLENTRY _EXPFUNC operator-(const complex _FAR &, double);
  111.     friend complex _RTLENTRY _EXPFUNC operator*(const complex _FAR &, const complex _FAR &);
  112.     friend complex _RTLENTRY _EXPFUNC operator*(const complex _FAR &, double);
  113.     friend complex _RTLENTRY _EXPFUNC operator*(double, const complex _FAR &);
  114.     friend complex _RTLENTRY _EXPFUNC operator/(const complex _FAR &, const complex _FAR &);
  115.     friend complex _RTLENTRY _EXPFUNC operator/(const complex _FAR &, double);
  116.     friend complex _RTLENTRY _EXPFUNC operator/(double, const complex _FAR &);
  117.     friend int _RTLENTRY _EXPFUNC operator==(const complex _FAR &, const complex _FAR &);
  118.     friend int _RTLENTRY _EXPFUNC operator!=(const complex _FAR &, const complex _FAR &);
  119.     complex _FAR & _RTLENTRY operator+=(const complex _FAR &);
  120.     complex _FAR & _RTLENTRY operator+=(double);
  121.     complex _FAR & _RTLENTRY operator-=(const complex _FAR &);
  122.     complex _FAR & _RTLENTRY operator-=(double);
  123.     complex _FAR & _RTLENTRY operator*=(const complex _FAR &);
  124.     complex _FAR & _RTLENTRY operator*=(double);
  125.     complex _FAR & _RTLENTRY operator/=(const complex _FAR &);
  126.     complex _FAR & _RTLENTRY operator/=(double);
  127.     complex _RTLENTRY operator+();
  128.     complex _RTLENTRY operator-();
  129.  
  130. // Implementation
  131. private:
  132.         double re, im;
  133. };
  134.  
  135.  
  136. // Inline complex functions
  137.  
  138. inline _RTLENTRY complex::complex(double __re_val, double __im_val)
  139. {
  140.     re = __re_val;
  141.     im = __im_val;
  142. }
  143.  
  144. inline _RTLENTRY complex::complex()
  145. {
  146. /* if you want your complex numbers initialized ...
  147.     re = im = 0;
  148. */
  149. }
  150.  
  151. inline complex _RTLENTRY complex::operator+()
  152. {
  153.     return *this;
  154. }
  155.  
  156. inline complex _RTLENTRY complex::operator-()
  157. {
  158.     return complex(-re, -im);
  159. }
  160.  
  161.  
  162. // Definitions of compound-assignment operator member functions
  163.  
  164. inline complex _FAR & _RTLENTRY complex::operator+=(const complex _FAR & __z2)
  165. {
  166.     re += __z2.re;
  167.     im += __z2.im;
  168.     return *this;
  169. }
  170.  
  171. inline complex _FAR & _RTLENTRY complex::operator+=(double __re_val2)
  172. {
  173.     re += __re_val2;
  174.     return *this;
  175. }
  176.  
  177. inline complex _FAR & _RTLENTRY complex::operator-=(const complex _FAR & __z2)
  178. {
  179.     re -= __z2.re;
  180.     im -= __z2.im;
  181.     return *this;
  182. }
  183.  
  184. inline complex _FAR & _RTLENTRY complex::operator-=(double __re_val2)
  185. {
  186.     re -= __re_val2;
  187.     return *this;
  188. }
  189.  
  190. inline complex _FAR & _RTLENTRY complex::operator*=(double __re_val2)
  191. {
  192.     re *= __re_val2;
  193.     im *= __re_val2;
  194.     return *this;
  195. }
  196.  
  197. inline complex _FAR & _RTLENTRY complex::operator/=(double __re_val2)
  198. {
  199.     re /= __re_val2;
  200.     im /= __re_val2;
  201.     return *this;
  202. }
  203.  
  204.  
  205. // Definitions of non-member complex functions
  206.  
  207. inline double _RTLENTRY real(const complex _FAR & __z)
  208. {
  209.     return __z.re;
  210. }
  211.  
  212. inline double _RTLENTRY imag(const complex _FAR & __z)
  213. {
  214.     return __z.im;
  215. }
  216.  
  217. inline complex _RTLENTRY conj(const complex _FAR & __z)
  218. {
  219.     return complex(__z.re, -__z.im);
  220. }
  221.  
  222. inline complex _RTLENTRY polar(double __mag, double __angle)
  223. {
  224.     return complex(__mag*cos(__angle), __mag*sin(__angle));
  225. }
  226.  
  227.  
  228. // Definitions of non-member binary operator functions
  229.  
  230. inline complex _RTLENTRY operator+(const complex _FAR & __z1, const complex _FAR & __z2)
  231. {
  232.     return complex(__z1.re + __z2.re, __z1.im + __z2.im);
  233. }
  234.  
  235. inline complex _RTLENTRY operator+(double __re_val1, const complex _FAR & __z2)
  236. {
  237.     return complex(__re_val1 + __z2.re, __z2.im);
  238. }
  239.  
  240. inline complex _RTLENTRY operator+(const complex _FAR & __z1, double __re_val2)
  241. {
  242.     return complex(__z1.re + __re_val2, __z1.im);
  243. }
  244.  
  245. inline complex _RTLENTRY operator-(const complex _FAR & __z1, const complex _FAR & __z2)
  246. {
  247.     return complex(__z1.re - __z2.re, __z1.im - __z2.im);
  248. }
  249.  
  250. inline complex _RTLENTRY operator-(double __re_val1, const complex _FAR & __z2)
  251. {
  252.     return complex(__re_val1 - __z2.re, -__z2.im);
  253. }
  254.  
  255. inline complex _RTLENTRY operator-(const complex _FAR & __z1, double __re_val2)
  256. {
  257.     return complex(__z1.re - __re_val2, __z1.im);
  258. }
  259.  
  260. inline complex _RTLENTRY operator*(const complex _FAR & __z1, double __re_val2)
  261. {
  262.     return complex(__z1.re*__re_val2, __z1.im*__re_val2);
  263. }
  264.  
  265. inline complex _RTLENTRY operator*(double __re_val